{{ extend '../_layout/layout.html' }}

{{ block 'header'}}
<link rel="stylesheet" href="/public/css/topic.css">
{{ /block }}>

{{ block 'content' }}
<div class="container">
    <div class="edit">
        <h1>发布新文章</h1>
        <hr>
        <form id="edit_form">
            <label for="author">作者</label>
            <input type="text" name="author" id="author" value="{{ user.nickname }}" readonly>
            <label for="title">标题</label>
            <input type="text" name="title" id="title" required>
            <label for="content">内容</label>
            <textarea name="content" id="content" cols="30" rows="10" required></textarea>
            <button>发布</button>
        </form>
    </div>
</div>
{{ /block }}

{{ block 'script' }}
<script>
    $('#edit_form').on('submit', function (e) {
        e.preventDefault()
        let formData = $(this).serialize()
        $.ajax({
            url: '/topics/edit',
            type: 'post',
            data: formData,
            dataType: 'json',
            success: function (data) {
                if(data.err_code === 0){
                    window.alert('发布成功！即将返回首页')
                    window.location.href = '/'
                }else{
                    window.alert('服务器繁忙！请稍后重试！！')
                }
            }
        })
    })
</script>
{{ /block }}